package com.xf.control;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.xf.constants.AppTypeEnum;
import com.xf.model.App;
import com.xf.model.AppType;
import com.xf.service.AppService;

public class IndexController extends Controller {

	@Inject
	private AppService appService;


	public void index() {
		List<App> allPus = appService.getAllPus();
		Map<Integer, List<App>> map = allPus.stream()
				.collect(Collectors.groupingBy(App::getType));

		List<AppType> appTypes = new ArrayList<>();
		for(Map.Entry<Integer, List<App>> entry:map.entrySet()){
			AppType appType = new AppType();
			appType.setType(entry.getKey());
			appType.setDesc(AppTypeEnum.getByType(entry.getKey()).getDesc());
			appType.setApps(entry.getValue());
			appTypes.add(appType);
		}
		Collections.sort(appTypes, Comparator.comparingInt(AppType::getType));

		setAttr("list",appTypes);

		render("index.html");
	}
}



